//
//  ViewController.m
//  Example12-1b
//
//  Created by 孙晓晔 on 2017/4/18.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ViewController.h"
#import <MapKit/MapKit.h>

#import "MyAnnotation.h"
#import "MyView.h"

@interface ViewController ()<MKMapViewDelegate>
@property(nonatomic,strong) MKMapView *mapView;
@property(nonatomic,strong) CLLocationManager *manager1;
@property (weak, nonatomic) IBOutlet UITextField *address;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusNotDetermined) {
        [self.manager1 requestAlwaysAuthorization];
    }
    
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
}

-(MKMapView *)mapView {
    
    if (_mapView == nil) {
        
        CGRect rect = self.view.bounds;
        rect.origin.y = 100;
        rect.size.height -= 100;
        
        _mapView = [[MKMapView alloc]initWithFrame:rect];
        _mapView.delegate = self;
        
        [self.view addSubview:self.mapView];
    }
    
    return _mapView;
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    
    NSLog(@"定位成功。");
    
    CLLocation *location = userLocation.location;
    
    CLGeocoder *coder = [[CLGeocoder alloc]init];
    
    [coder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *mark = [placemarks lastObject];
        
        NSDictionary *dict = mark.addressDictionary;
        
        NSLog(@"地址是 %@", dict);
        
    }];
}
- (IBAction)find:(id)sender {
    
    CLGeocoder *coder = [[CLGeocoder alloc]init];
    
    [coder geocodeAddressString:self.address.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        for (CLPlacemark *mark in placemarks) {
            NSDictionary *dict = mark.addressDictionary;
            
            NSLog(@"查找成功： %@", dict[@"Name"]);
            
            [self.mapView setCenterCoordinate:mark.location.coordinate animated:YES];
            
            [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(mark.location.coordinate, 100, 100) animated:YES];
            
            MyAnnotation *annotation = [[MyAnnotation alloc]init];
            
            annotation.coordinate = mark.location.coordinate;
            annotation.title = dict[@"Name"];
            annotation.subtitle = @"这是子标题";
            
            [self.mapView addAnnotation:annotation];
        }
        
    }];
    
    [self.address resignFirstResponder];
    
}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    
    MyView *view = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 30, 15)];
    
    view.backgroundColor = [UIColor redColor];
    
    return view;
}

-(CLLocationManager *)manager1 {
    if (_manager1 == nil) {
        _manager1 = [[CLLocationManager alloc]init];
    }
    
    return _manager1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
